package org.mijia.dotsbox.rcp;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.mijia.dotsbox.DotsboxApp;
import org.mijia.dotsbox.DotsboxAppGui;
import org.mijia.dotsbox.core.ChannelDao;
import org.mijia.dotsbox.core.FolderDao;
import org.mijia.dotsbox.core.model.Channel;
import org.mijia.dotsbox.core.model.Entity;
import org.mijia.dotsbox.core.model.Folder;

public class ChannelTreeModel {

	private TreeNode rootnode;
	
	public ChannelTreeModel() {
	}
	
	public TreeNode getRoot() {
		if (rootnode == null) {
			rootnode = new TreeNode(DotsboxApp.getInstance().getRootFolder());
		}
		return rootnode;
	}
	
	public boolean containsChannel(String link) {
		return containsChild(rootnode, link, 0, 2);
	}
	
	public boolean containsFolder(String folderName) {
		if (folderName.equals(DotsboxApp.ROOT_FOLDER_ID))
			return true;
		
		return containsChild(rootnode, folderName, 0, 1);
	}

	private boolean containsChild(TreeNode node, String id, int level, int depth) {
		if (depth == level)
			return false;
		
		List<TreeNode> children = node.getChildren();
		for (TreeNode child: children) {
			if (child.getEntity().equals(id)) 
				return true;
			
			if(containsChild(child, id, level + 1, depth))
				return true;
		}
		return false;
	}
	
	public void addChild(TreeNode node, Entity entity) {
		List<TreeNode> children = node.getChildren();
		TreeNode child = new TreeNode(entity);
		child.setParent(node);
		children.add(child);
	}

	public void removeChild(TreeNode node, Entity entity) {
		// TODO: remove child
	}
	
	@SuppressWarnings("unchecked")
	public TreeNode[] getChildren(TreeNode node) {
		List<TreeNode> children = node.getChildren();		
		TreeNode[] results = new TreeNode[children.size()];
		return children.toArray(results);
	}
	
	public static class TreeNode {
		
		private TreeNode parent;
		private List<TreeNode> children;
		private Entity entity;
		
		public TreeNode(Entity entity) {
			this.entity = entity;
			children = null;
		}

		Entity getEntity() {
			return entity;
		}
		
		TreeNode getParent() {
			return parent;
		}

		void setParent(TreeNode parent) {
			this.parent = parent;
		}

		List<TreeNode> getChildren() {
			if (children == null) {
				children = new ArrayList<TreeNode>();
				Entity entity = getEntity();
				if (entity instanceof Folder) {
					FolderDao fdao = (FolderDao) DotsboxApp.getInstance().getProfile().getDao(Folder.class);
					List<String> ids = fdao.findByParentId(entity.getId());
					if (ids != null) {
						for (String id: ids) {
							Folder folder = fdao.find(id);
							TreeNode childNode = new TreeNode(folder);
							childNode.setParent(this);
							children.add(childNode);
						}
					}
					
					ChannelDao cdao = (ChannelDao) DotsboxApp.getInstance().getProfile().getDao(Channel.class);
					ids = cdao.findByFolderId(entity.getId());
					if (ids != null) {
						for (String id: ids) {
							Channel channel = cdao.find(id);
							TreeNode childNode = new TreeNode(channel);
							childNode.setParent(this);
							children.add(childNode);
						}
					}
				}
			}
			return children;
		}

		void setChildren(List<TreeNode> children) {
			this.children = children;
		}
		
		Image getImage() {
			if (entity instanceof Folder) {
				return DotsboxAppGui.getImage("icons/views/folder.png");
			} else if (entity instanceof Channel) {
				return DotsboxAppGui.getImage("icons/views/feed.png");
			}
			return null;
		}

		String getText() {
			if (entity instanceof Folder) {
				Folder f = (Folder) entity;
				return f.getName() + " (" + getChildren().size() + ")";
			}
			return entity.getId();
		}

		Font getFont() {
			return null;
		}

		Color getBackground() {
			return null;
		}

		Color getForeground() {
			return null;
		}
		
	}

}
